from qtpy import QtCore

from untitled import select_call_Dialog
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QListWidget, QCheckBox, QListWidgetItem, QMessageBox
from PyQt5.QtCore import Qt
from select_web_data import user_login


class Manage(QMainWindow, select_call_Dialog):

    def __init__(self, parent=None, *args, **kwargs):
        super(Manage, self).__init__(parent, *args, **kwargs)
        # 显示背景图，因为pyqt5设置问题
        self.setAttribute(Qt.WA_StyledBackground, True)
        self.setupUi(self)
        # 运行键
        self.pushButton_3.clicked.connect(self.exit)
        self.pushButton.clicked.connect(self.query_site)



    def exit(self):
        self.close()

    def clear(self):
        for i in range(self.row_num):
            self.qCheckBox[i].setChecked(False)

    def showMessage(self):  #2.状态更改时
        Outputlist = self.Selectlist()
        self.qLineEdit.setReadOnly(False)
        self.qLineEdit.clear()
        self.qLineEdit.setText(" ")
        self.qLineEdit.setReadOnly(True)

    def addQCheckBox(self, i):  #1.遍历生成每一项
        self.qCheckBox.append(QCheckBox())
        qItem = QListWidgetItem(self.qListWidget)
        self.qCheckBox[i].setText(self.site_list[i])
        self.qListWidget.setItemWidget(qItem, self.qCheckBox[i])

    def Selectlist(self):  #3.输出选中列表
        Outputlist = []
        for i in range(0, self.row_num):
            if self.qCheckBox[i].isChecked() == True:
                print(self.qCheckBox[i].text())
                Outputlist.append(self.qCheckBox[i].text())
        return Outputlist

    def query_site(self):
        # self.pushButton.setText("查询中....")
        username = self.lineEdit.text()  # 账号
        password = self.lineEdit_2.text() # 密码
        print(username, password)


        #查询该账号下站点
        self.site_list = user_login(username, password)
        # self.pushButton.setText("查询该账号站点")
        print(self.site_list)

        if self.site_list == "密码错误":
            self.qCheckBox =[]
            self.qLineEdit = QLineEdit()
            self.qLineEdit.setReadOnly(True)
            self.qListWidget = QListWidget()
            self.comboBox.setModel(self.qListWidget.model())
            self.comboBox.setView(self.qListWidget)
            print("错误处理")
            QMessageBox.information(None, "提示", "账号或密码错误", QMessageBox.Yes)

        else:
            #生成下拉复选框
            self.row_num = len(self.site_list)
            self.qCheckBox = []
            self.qLineEdit = QLineEdit()
            self.qLineEdit.setReadOnly(True)
            self.qListWidget = QListWidget()
            for i in range(0, self.row_num):
                self.addQCheckBox(i)
                self.qCheckBox[i].stateChanged.connect(self.showMessage)
            self.comboBox.setModel(self.qListWidget.model())
            self.comboBox.setView(self.qListWidget)
            self.comboBox.setLineEdit(self.qLineEdit)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    manage = Manage()
    manage.show()
    sys.exit(app.exec())